---@type SourceModifierList
local SourceModifierList = require("util/sourcemodifierlist")

local MAX_LOW_VALUE = 65535
local rewards = require 'core_webber_the_trainer/data/webbert_soul_buy_items'
local totoal_buy_items = #rewards -- 商品目前总数

local props = {
    soul_high = function(self, value)
        self.inst.replica.webbert_soul.soul_high:set(value)
    end,
    soul_low = function(self, value)
        self.inst.replica.webbert_soul.soul_low:set(value)
    end,
}
for i=1,totoal_buy_items do
    props['buy_times_'..i] = function(self, value)
        self.inst.replica.webbert_soul['buy_times_'..i]:set(value)
    end
end

---@class components
---@field webbert_soul component_webbert_soul

---@class component_webbert_soul
---@field inst ent
---@field soul_high integer
---@field soul_low integer
---@field gain_modifier SourceModifierList # 血契获取数量增益,加算修饰
local webbert_soul = Class(
---@param self component_webbert_soul
function(self, inst)
    self.inst = inst
    self.soul_high = 0
    self.soul_low = 0
    self.gain_modifier = SourceModifierList(inst,0,SourceModifierList.additive)

    for i = 1,totoal_buy_items do
        self['buy_times_'..i] = 0
    end
end,
nil,
props)

function webbert_soul:OnSave()
    local sav_data = {
        soul_high = self.soul_high,
        soul_low = self.soul_low,
    }
    for i=1,totoal_buy_items do
        sav_data['buy_times_'..i] = self['buy_times_'..i]

        local fn_save = rewards[i].fn_save
        if fn_save then
            local res = fn_save(self.inst)
            for k,v in pairs(res) do
                sav_data[k] = v
            end
        end

    end
    return sav_data
end

function webbert_soul:OnLoad(data)
    self.soul_high = data.soul_high or 0
    self.soul_low = data.soul_low or 0

    for i=1,totoal_buy_items do
        self['buy_times_'..i] = data['buy_times_'..i] or 0

        local fn_load = rewards[i].fn_load
        if fn_load then
            fn_load(self.inst,data)
        end
    end
end

function webbert_soul:SetSoul(value)
    value = math.floor(value) -- 确保值为整数
    local _max_low_value = MAX_LOW_VALUE -- 假设低位部分最大为65535（16位）
    self.soul_high = math.floor(value / (_max_low_value + 1))
    self.soul_low = value % (_max_low_value + 1)
end

function webbert_soul:GetSoul()
    local _max_low_value = MAX_LOW_VALUE -- 必须与SetSoul中使用的值一致
    return self.soul_high * (_max_low_value + 1) + self.soul_low
end

---尝试增加或减少血契值,如果不够会返回原因
---@param delta number
function webbert_soul:TryDelta(delta)
    delta = math.floor(delta) -- 确保delta为整数
    if delta > 0 then
        delta = delta * (1 + (self.gain_modifier:Get() or 0))
    end
    local cur = self:GetSoul()
    -- 如果剩余的血契值不够扣,则失败
    if delta < 0 and cur < math.abs(delta) then
        return false
    end
    local now = cur + delta
    self:SetSoul(now)
    return true
end

function webbert_soul:Buy()

end

---血契获取数量增益,加算修饰
---@param src ent|string
---@param m number
---@param key string
function webbert_soul:SetModifier(src,m,key)
    self.gain_modifier:SetModifier(src,m,key)
end

---移除加算修饰
---@param src ent|string
---@param key string
function webbert_soul:RemoveModifier(src,key)
    self.gain_modifier:RemoveModifier(src,key)
end

return webbert_soul